import React, { PureComponent } from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { connect } from 'dva';
import { FormGroup, DATA_ENTRY, codeType } from '../../../../comps';

@Form.create()
@connect(({ globalM }) => ({
  globalM,
}))
export default class myModal extends PureComponent {
  state = {
    assetTypeList: [
      { value: '2', title: '收/受益权' },
      { value: '9', title: '其他' },
    ],
    assetType: undefined,
    selectTrading: undefined,
  };
  componentDidMount() {
    this.props?.onRef(this);
    const { editRow } = this.props;
    if (editRow) {
      this.setState({
        assetType: this.props.editRow?.assetType,
      });
    }
  }
  // 获取参数
  getInfo = () => {
    const { selectTrading } = this.state;
    const { editRow } = this.props;
    let params = undefined;
    const {
      form: { validateFields },
    } = this.props;
    validateFields((error, values) => {
      if (error) {
        return;
      }
      if (editRow) {
        values.id = editRow.id;
        values.tradingCode = editRow.tradingCode;
      } else {
        values.tradingCode = selectTrading?.tradingCode || values.tradingCode;
      }
      params = values;
    });
    return params;
  };
  // 证券代码变化 获取回填
  tradingCodeChange = (val) => {
    // console.log('first tradingCodeChange', val);
    const {
      dispatch,
      form: { setFieldsValue },
    } = this.props;
    this.setState({
      selectTrading: val,
    });
    if (this.props.editRow) return;
    // 查询回填
    if (val?.tradingCode) {
      dispatch({
        type: 'manNonStandardMaintenanceM/backfillDetail',
        payload: {
          tradingCode: val.tradingCode,
          exchangeCode: val.exchangeCode,
        },
        callback: (msg, _data) => {
          if (msg === 'success') {
            this.setState({
              assetType: _data?.assetType || undefined,
            });
            setFieldsValue({
              assetType: _data?.assetType || undefined,
              jgbb_ssyqlx: _data?.jgbb_ssyqlx || undefined,
              jgbb_txbffl: _data?.jgbb_txbffl || undefined,
            });
          }
        },
      });
    }
  };
  //清空表单数据
  reset = () => {
    const {
      form: { resetFields },
    } = this.formContent.props;
    resetFields();
  };
  render() {
    const { assetTypeList, assetType } = this.state;
    const {
      form,
      globalM: {
        dataDictionary: { ASSETS_TXBFFL = [], NONSTANDAR_JGBB_SSYQLX1 = [] },
      },
      editRow,
    } = this.props;
    const formProps = {
      form,
      autoLayout: false,
      rowNumber: 2,
      onRef: (ref) => (this.formContent = ref),
      widthSet: 840,
      dataSource: [
        {
          // hide: !!editRow,
          title: '证券代码',
          type: DATA_ENTRY.SECURITIESCODE,
          codeType: codeType.fb,
          dataIndex: 'tradingCode',
          isInput: true,
          disabled: !!editRow,
          initialValue: editRow?.tradingCode ? editRow?.exchangeCode + '-' + editRow?.tradingCode : undefined,
          rules: [{ required: true, message: '请输入证券代码！' }],
          onChange: this.tradingCodeChange,
          positionBody: true,
        },
        {
          title: '资产类别',
          dataIndex: 'assetType',
          type: DATA_ENTRY.SELECT,
          positionBody: true,
          allowClear: true,
          dataSource: assetTypeList,
          initialValue: editRow?.assetType,
          rules: [{ required: true, message: '请选择资产类别' }],
          onChange: (e) => {
            this.setState({ assetType: e });
          },
        },
        {
          title: '收/受益权类型',
          dataIndex: 'jgbb_ssyqlx',
          type: DATA_ENTRY.SELECT,
          positionBody: true,
          allowClear: true,
          dataSource: (NONSTANDAR_JGBB_SSYQLX1 || []).map((item) => {
            return { value: item.code, title: item.value };
          }),
          initialValue: editRow?.jgbb_ssyqlx,
          rules: [{ required: assetType == '2', message: '请选择资产类别' }],
        },
        {
          title: '投向部分分类',
          dataIndex: 'jgbb_txbffl',
          type: DATA_ENTRY.SELECT,
          positionBody: true,
          allowClear: true,
          dataSource: (ASSETS_TXBFFL || []).map((item) => {
            return { value: item.code, title: item.value };
          }),
          initialValue: editRow?.jgbb_txbffl,
          rules: [{ required: assetType == '2', message: '请选择资产类别' }],
        },
      ],
    };
    return <FormGroup {...formProps} />;
  }
}
